package com.example.servcie.impl;

import com.example.common.constants.MSG_ALERT_ENUM;
import com.example.common.controllerdto.EbizMainProductSaidListDTO;
import com.example.common.dto.TransResult;
import com.example.common.exception.BusinessException;
import com.example.common.util.CollectionUtil;
import com.example.common.util.StringUtil;
import com.example.mapper.EbizMainProductListMapper;
import com.example.model.EbizMainProductList;
import com.example.model.EbizMainProductListExample;
import com.example.servcie.EbizMainProductListService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ：czx
 * @date ：Created in 2021/6/24 18:02
 * @modified By：
 */
@Service
public class EbizMainProductListServiceImpl implements EbizMainProductListService {

   @Autowired
    private EbizMainProductListMapper ebizMainProductListMapper;

    @Override
    public TransResult getEbizMainProductList(EbizMainProductSaidListDTO ebizMainProductSaidListDTO) {
        TransResult transResult=new TransResult();
        //1-先校验分页是否为空
        if (null==ebizMainProductSaidListDTO.getPageNum()||null==ebizMainProductSaidListDTO.getPageSize()){
            throw new BusinessException(MSG_ALERT_ENUM.PAGENUM_AND_PAGESIZE_NOT_NULL);
        }
        //2-分页数据不为空,则set进PageHepler
        PageHelper.startPage(ebizMainProductSaidListDTO.getPageNum(),ebizMainProductSaidListDTO.getPageSize());

        //3-使用条件构造器进行产品的名称的模糊查询,并以更新时间做降序排序
        EbizMainProductListExample ebizMainProductListExample=new EbizMainProductListExample();
        EbizMainProductListExample.Criteria criteria=ebizMainProductListExample.createCriteria();
        EbizMainProductList ebizMainProductList=ebizMainProductSaidListDTO.getEbizMainProductList();
        if (null!=ebizMainProductList && StringUtil.isNotBlank(ebizMainProductList.getProductName())){
                criteria.andProductNameLike("%"+ebizMainProductList.getProductName()+"%");
        }
        ebizMainProductListExample.setOrderByClause("update_time Desc");
        List<EbizMainProductList> ebizMainProductListLists =ebizMainProductListMapper.selectByExample(ebizMainProductListExample);

        //4-判断是否查到数据,为空则返回报错
        if (CollectionUtil.isEmpty(ebizMainProductListLists)){
            throw new BusinessException(MSG_ALERT_ENUM.PRODUCTSAID_NOT_EXIST);
        }

        //5-不为空,返回分页和查询的数据
        PageInfo<EbizMainProductList> ebizMainProductListPageInfo=new PageInfo<>(ebizMainProductListLists);
        transResult.setObject(ebizMainProductListPageInfo);
        return transResult;


    }
}
